/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.WbEmergencyHelp;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.WbInspectRecord;
import com.zhuoyue.elmt.service.WbInspectRecordService;

import java.util.ArrayList;
import java.util.List;

/**
 * 检验记录Controller
 *
 * @author JiangYH
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/wbInspectRecord")
public class WbInspectRecordController extends BaseController {

    @Autowired
    private WbInspectRecordService wbInspectRecordService;

    @ModelAttribute
    public WbInspectRecord get(@RequestParam(required = false) String id) {
        WbInspectRecord entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = wbInspectRecordService.get(id);
        }
        if (entity == null) {
            entity = new WbInspectRecord();
        }
        return entity;
    }

    @RequiresPermissions("elmt:wbInspectRecord:view")
    @RequestMapping(value = {"list", ""})
    public String list(WbInspectRecord wbInspectRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<WbInspectRecord> page = wbInspectRecordService.findPage(new Page<WbInspectRecord>(request, response), wbInspectRecord);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/wbInspectRecordList";
    }

    @RequiresPermissions("elmt:wbInspectRecord:view")
    @RequestMapping(value = "form")
    public String form(WbInspectRecord wbInspectRecord, Model model) {
        model.addAttribute("wbInspectRecord", wbInspectRecord);
        return "zhuoyue/elmt/wbInspectRecordForm";
    }

    @RequiresPermissions("elmt:wbInspectRecord:edit")
    @RequestMapping(value = "save")
    public String save(WbInspectRecord wbInspectRecord, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, wbInspectRecord)) {
            return form(wbInspectRecord, model);
        }
        wbInspectRecordService.save(wbInspectRecord);
        addMessage(redirectAttributes, "保存检验记录成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/wbInspectRecord/?repage";
    }

    @RequiresPermissions("elmt:wbInspectRecord:edit")
    @RequestMapping(value = "delete")
    public String delete(WbInspectRecord wbInspectRecord, RedirectAttributes redirectAttributes) {
        wbInspectRecordService.delete(wbInspectRecord);
        addMessage(redirectAttributes, "删除检验记录成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/wbInspectRecord/?repage";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/mobileget/{str}")
    public String mobileget(@PathVariable String str, HttpServletRequest request, HttpServletResponse response, Model model) {
        ResultData<List<WbInspectRecord>> resultData = new ResultData();
        List<WbInspectRecord> list = new ArrayList();
        try {
            WbInspectRecord entity = null;
            if (StringUtils.isNotBlank(str)) {
                entity = wbInspectRecordService.get(str);
            }

            list.add(entity);
            resultData.setList(list);

            resultData.setCode("SUCC");
            resultData.setMessage("获取成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("获取异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(value = "mobilelist")
    public String mobilelist(WbInspectRecord wbInspectRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        ResultData<List<WbInspectRecord>> resultData = new ResultData();
        try {
            Page<WbInspectRecord> page = wbInspectRecordService.findPage(new Page<WbInspectRecord>(request, response), wbInspectRecord);
            resultData.setList(page.getList());

            resultData.setCode("SUCC");
            resultData.setMessage("查询成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("查询异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(value = "mobilesave")
    public String mobilesave(WbInspectRecord wbInspectRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
        ResultData<List<WbInspectRecord>> resultData = new ResultData();
        List<WbInspectRecord> list = new ArrayList();
        try {
            wbInspectRecordService.save(wbInspectRecord);
            list.add(wbInspectRecord);
            resultData.setList(list);

            resultData.setCode("SUCC");
            resultData.setMessage("保存成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("保存异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(value = "mobiledelete")
    public String mobiledelete(WbInspectRecord wbInspectRecord, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        ResultData<List<WbInspectRecord>> resultData = new ResultData();
        List<WbInspectRecord> list = new ArrayList();
        try {
            wbInspectRecordService.delete(wbInspectRecord);
            list.add(wbInspectRecord);
            resultData.setList(list);

            resultData.setCode("SUCC");
            resultData.setMessage("删除成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("删除异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }
}